package com.wushixin.springdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wushixin.springdemo.dto.BookDto;
import com.wushixin.springdemo.pojo.Book;
import com.wushixin.springdemo.pojo.User;
import com.wushixin.springdemo.pojo.utils.Result;
import com.wushixin.springdemo.service.BookService;
import com.wushixin.springdemo.service.UserService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping(value = "/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping
    public Result GetAll() {
        return  new Result(bookService.getAll(),bookService.list()!=null?true:false);
    }
    @GetMapping("{id}")
    public Result GetById(@PathVariable Integer id) {
        return new Result(bookService.getById(id),true);
    }

    @PostMapping
    public Result saveStudent(@RequestBody Book book) {
        return new Result(null,bookService.save(book));
    }

    @DeleteMapping("{id}")
    public Result deleteStudent(@PathVariable Integer id) {
        boolean flag = bookService.removeById(id);
        return new Result(null,flag);
    }

    @PutMapping
    public Result updateStudent(@RequestBody Book book) {
        Boolean flag = bookService.updateById(book);

        return new Result(flag,flag?"添加成功":"添加失败");
    }

    @GetMapping("{current}/{pageSize}")
    public Result pageStudent(@PathVariable Integer current,@PathVariable Integer pageSize,String name) {
        Page<BookDto> page = bookService.getPage(current,pageSize,name);
        return new Result(page,true);
    }
}
